<?php

class FeedbackController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $form = new Application_Form_Feedback();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $values['date'] = strtotime(date('c'));
                $feedbackDb = new Admin_Model_Feedback();
                $feedbackDb->insert(array(
                    'details' => json_encode($values)
                ));
                $this->view->isSent = true;
            }
        }
    }

    public function readAction() {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect("/admin/login");
        }
        $this->_helper->layout->setLayout("admin");
        $feedbackDb = new Admin_Model_Feedback();
        $fetchAll = $feedbackDb->fetchAll();
        $this->view->fetchAll = array_reverse($fetchAll);
    }

    public function verifyAction() {
        $mobile = $this->getRequest()->getParam("mobile");
        $message = $this->getRequest()->getParam("message");
        $this->view->message = $message;
        $this->_helper->layout()->disableLayout();
        $form = new Application_Form_Verify();
        $form->number->setValue($mobile);
        $this->view->form = $form;
        $this->view->mobile = $mobile;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $mobileNumber = $values['number'];
                $code = $values['code'];
                $mobileValidationDb = new Admin_Model_Mobilevalidation();
                $byMobile = $mobileValidationDb->getByMobile($mobileNumber);
                if (sizeof($byMobile) != 0) {
                    if ($byMobile['isVerified']) {
                        $this->view->isErrorMessage = true;
                        $this->view->errorMessage = "You number is already verified.";
                    } else {
                        if ($byMobile['code'] == $code) {
                            $mobileValidationDb->update(array('verified' => true), $byMobile['id']);
                            $this->view->isSuccess = true;
                            $optionsDb = new Admin_Model_Options();
                            $fullOnSmsDetails = $optionsDb->getFullOnSmsDetails();
                            $message = $byMobile['message'];
                            $messageLength = strlen($message); #check message length
                            if ($messageLength > 140) {
                                $sendMessage = array(
                                    substr($message, 0, 139),
                                    substr($message, 140, $messageLength - 140),
                                );
                            } else {
                                $sendMessage = array($message);
                            }
                            foreach ($sendMessage as $message) {
                                $isSent = $this->sendSMSFunction($fullOnSmsDetails, $mobileNumber, $message);
                            }

                            $smsData = array(
                                'mobile' => $mobileNumber,
                                'sent' => $isSent,
                                'isDnd' => false,
                            );
                            $notificationDb = new Admin_Model_Sentnotifications();
                            $notificationDb->update(array(
                                'sms' => json_encode($smsData),
                                    ), $byMobile['notificationId']);
                            //  $mobileValidationDb->update(array('message_queue' => ''), $byMobile['id']);
                        } else {
                            $this->view->isErrorMessage = true;
                            $this->view->errorMessage = "Wrong code entered.";
                        }
                    }
                } else {
                    $this->view->isErrorMessage = true;
                    $this->view->errorMessage = "This mobile number does not exists.";
                }
            }
        }
    }

    public function sendSMSFunction($fullOnSmsDetails, $mobile, $message) {
        $url = "http://" . $_SERVER['SERVER_NAME'] . "/Sms/sendsms.php";
        $finalUrl = $url . '?uid=' . $fullOnSmsDetails['username'] . '&pwd=' . $fullOnSmsDetails['password'] . '&phone=' . $mobile . '&msg=' . urlencode($message);
        // echo $finalUrl;                    
        // Generate curl request
        $session = curl_init($finalUrl);
        // Tell curl to use HTTP POST
        curl_setopt($session, CURLOPT_POST, false);
        curl_setopt($session, CURLOPT_HEADER, false);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        // obtain response
        $response = curl_exec($session);
        $founded = false;
        if (substr($response, 0, 4) == 'true') {
            $founded = true;
        }
        curl_close($session);
        return $founded;
    }

    public function resendverificationcodeAction() {
        $mobile = $this->getRequest()->getParam("mobile");
        $this->_helper->layout()->disableLayout();
        $mobileValidationDb = new Admin_Model_Mobilevalidation();
        $byMobile = $mobileValidationDb->getByMobile($mobile);
        $message = "Your validation code is " . $byMobile['code'].".";
        $optionsDb = new Admin_Model_Options();
        $fullOnSmsDetails = $optionsDb->getFullOnSmsDetails();
        //$isSent = $this->sendSMSFunction($fullOnSmsDetails, $mobile, $message);
        $isSent = true;
        if ($isSent) {
            $this->_redirect("/verify/$mobile?message=sent");
        }
    }

}

